--[[	$Id: iLvl.lua 2577 2012-09-22 06:10:30Z sdkyron@gmail.com $	]]

local _, caelCore = ...

local itemSlots = {"HeadSlot", "NeckSlot", "ShoulderSlot", "ChestSlot", "WaistSlot", "LegsSlot", "FeetSlot", "WristSlot", "HandsSlot",
					"Finger0Slot", "Finger1Slot", "Trinket0Slot", "Trinket1Slot", "BackSlot", "MainHandSlot", "SecondaryHandSlot"
}

local GetiLvlBySlot = function(slot, count, total)
    local id = GetInventorySlotInfo(slot)
    local link = GetInventoryItemLink("player", id)

    if link == nil then
      return count, total
    else
      local _, link, _, ilvl = GetItemInfo(link)

      count = count + 1
      total = total + ilvl

      return count, total
    end
end

local getiLvl = function()
    local itemiLvl = 0
    local totaliLvl = 0

	for k, v in pairs(itemSlots) do
		itemiLvl, totaliLvl = GetiLvlBySlot(v, itemiLvl, totaliLvl)
	end

    if itemiLvl < 15 then
      itemiLvl = 15
    end

    return math.floor(totaliLvl/itemiLvl)

end

SLASH_GETILVL1 = "/ilvl"
SlashCmdList["GETILVL"] = function()
	print(format("|cffD7BEA5cael|rCore: eQiLvl: %d", getiLvl()))
end